home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / jpeg-6a / jmemmac.c < prev    next >
C/C++ Source or Header  |  1996-02-07  |  6KB  |  200 lines

  1. /*
  2.  * jmemmac.c
  3.  *
  4.  * Copyright (C) 1992-1996, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * jmemmac.c provides an Apple Macintosh implementation of the system-
  9.  * dependent portion of the JPEG memory manager.
  10.  *
  11.  * jmemmac.c uses the Macintosh toolbox routines NewPtr and DisposePtr
  12.  * instead of malloc and free.  It accurately determines the amount of
  13.  * memory available by using CompactMem.  Notice that if left to its
  14.  * own devices, this code can chew up all available space in the
  15.  * application's zone, with the exception of the rather small "slop"
  16.  * factor computed in jpeg_mem_available().  The application can ensure
  17.  * that more space is left over by reducing max_memory_to_use.
  18.  *
  19.  * Large images are swapped to disk using temporary files created with
  20.  * tmpfile(); that part of the module is the same as in jmemansi.c.
  21.  * Metrowerks CodeWarrior's implementation of tmpfile() isn't quite what
  22.  * we want: it puts the files in the local directory and makes them
  23.  * user-visible -- and only deletes them when the application quits,
  24.  * which means they stick around in the event of a crash.
  25.  * It would be better to create the temp files in the system's temporary
  26.  * items folder.  Perhaps someday we'll get around to doing that.
  27.  *
  28.  * Contributed by Sam Bushell (jsam@iagu.on.net).
  29.  */
  30.  
  31. #define JPEG_INTERNALS
  32. #include "jinclude.h"
  33. #include "jpeglib.h"
  34. #include "jmemsys.h"        /* import the system-dependent declarations */
  35.  
  36. #include <Memory.h>        /* we use the MacOS memory manager */
  37.  
  38. #ifndef SEEK_SET        /* pre-ANSI systems may not define this; */
  39. #define SEEK_SET  0        /* if not, assume 0 is correct */
  40. #endif
  41.  
  42.  
  43. /*
  44.  * Memory allocation and freeing are controlled by the MacOS library
  45.  * routines NewPtr() and DisposePtr(), which allocate fixed-address
  46.  * storage.  Unfortunately, the IJG library isn't smart enough to cope
  47.  * with relocatable storage.
  48.  */
  49.  
  50. GLOBAL(void *)
  51. jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
  52. {
  53.   return (void *) NewPtr(sizeofobject);
  54. }
  55.  
  56. GLOBAL(void)
  57. jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
  58. {
  59.   DisposePtr((Ptr) object);
  60. }
  61.  
  62.  
  63. /*
  64.  * "Large" objects are treated the same as "small" ones.
  65.  * NB: we include FAR keywords in the routine declarations simply for
  66.  * consistency with the rest of the IJG code; FAR should expand to empty
  67.  * on rational architectures like the Mac.
  68.  */
  69.  
  70. GLOBAL(void FAR *)
  71. jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
  72. {
  73.   return (void FAR *) NewPtr(sizeofobject);
  74. }
  75.  
  76. GLOBAL(void)
  77. jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
  78. {
  79.   DisposePtr((Ptr) object);
  80. }
  81.  
  82.  
  83. /*
  84.  * This routine computes the total memory space available for allocation.
  85.  */
  86.  
  87. GLOBAL(long)
  88. jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
  89.             long max_bytes_needed, long already_allocated)
  90. {
  91.   long limit = cinfo->mem->max_memory_to_use - already_allocated;
  92.   long slop, mem;
  93.  
  94.   /* Don't ask for more than what application has told us we may use */
  95.   if (max_bytes_needed > limit && limit > 0)
  96.     max_bytes_needed = limit;
  97.   /* Find whether there's a big enough free block in the heap.
  98.    * CompactMem tries to create a contiguous block of the requested size,
  99.    * and then returns the size of the largest free block (which could be
  100.    * much more or much less than we asked for).
  101.    * We add some slop to ensure we don't use up all available memory.
  102.    */
  103.   slop = max_bytes_needed / 16 + 32768L;
  104.   mem = CompactMem(max_bytes_needed + slop) - slop;
  105.   if (mem < 0)
  106.     mem = 0;            /* sigh, couldn't even get the slop */
  107.   /* Don't take more than the application says we can have */
  108.   if (mem > limit && limit > 0)
  109.     mem = limit;
  110.   return mem;
  111. }
  112.  
  113.  
  114. /*
  115.  * Backing store (temporary file) management.
  116.  * Backing store objects are only used when the value returned by
  117.  * jpeg_mem_available is less than the total space needed.  You can dispense
  118.  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  119.  */
  120.  
  121.  
  122. METHODDEF(void)
  123. read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  124.             void FAR * buffer_address,
  125.             long file_offset, long byte_count)
  126. {
  127.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  128.     ERREXIT(cinfo, JERR_TFILE_SEEK);
  129.   if (JFREAD(info->temp_file, buffer_address, byte_count)
  130.       != (size_t) byte_count)
  131.     ERREXIT(cinfo, JERR_TFILE_READ);
  132. }
  133.  
  134.  
  135. METHODDEF(void)
  136. write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  137.              void FAR * buffer_address,
  138.              long file_offset, long byte_count)
  139. {
  140.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  141.     ERREXIT(cinfo, JERR_TFILE_SEEK);
  142.   if (JFWRITE(info->temp_file, buffer_address, byte_count)
  143.       != (size_t) byte_count)
  144.     ERREXIT(cinfo, JERR_TFILE_WRITE);
  145. }
  146.  
  147.  
  148. METHODDEF(void)
  149. close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
  150. {
  151.   fclose(info->temp_file);
  152.   /* Since this implementation uses tmpfile() to create the file,
  153.    * no explicit file deletion is needed.
  154.    */
  155. }
  156.  
  157.  
  158. /*
  159.  * Initial opening of a backing-store object.
  160.  *
  161.  * This version uses tmpfile(), which constructs a suitable file name
  162.  * behind the scenes.  We don't have to use info->temp_name[] at all;
  163.  * indeed, we can't even find out the actual name of the temp file.
  164.  */
  165.  
  166. GLOBAL(void)
  167. jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
  168.              long total_bytes_needed)
  169. {
  170.   if ((info->temp_file = tmpfile()) == NULL)
  171.     ERREXITS(cinfo, JERR_TFILE_CREATE, "");
  172.   info->read_backing_store = read_backing_store;
  173.   info->write_backing_store = write_backing_store;
  174.   info->close_backing_store = close_backing_store;
  175. }
  176.  
  177.  
  178. /*
  179.  * These routines take care of any system-dependent initialization and
  180.  * cleanup required.
  181.  */
  182.  
  183. GLOBAL(long)
  184. jpeg_mem_init (j_common_ptr cinfo)
  185. {
  186.   /* max_memory_to_use will be initialized to FreeMem()'s result;
  187.    * the calling application might later reduce it, for example
  188.    * to leave room to invoke multiple JPEG objects.
  189.    * Note that FreeMem returns the total number of free bytes;
  190.    * it may not be possible to allocate a single block of this size.
  191.    */
  192.   return FreeMem();
  193. }
  194.  
  195. GLOBAL(void)
  196. jpeg_mem_term (j_common_ptr cinfo)
  197. {
  198.   /* no work */
  199. }
  200.